<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>server</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container" id="app">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="" lay-filter="table-search">
                    <div class="layui-form-item">

                    <div class="layui-inline">
                        <label class="layui-form-label" >渠道类型</label>
                        <div class="layui-input-inline">
                            <div id="serverGroup"></div>
                        </div>
                    </div>

                    <div class="layui-inline" >
                        <label class="layui-form-label">记录时间</label>
                        <div class="layui-input-inline">
                            <input type="text" name="createDateBegin" id="createTime0"
                                   placeholder="开始时间" autocomplete="off" class="layui-input" />
                            <input type="text" name="createDateEnd" id="createTime1"
                                   placeholder="截止时间" autocomplete="off" class="layui-input" />
                        </div>
                    </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
       <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="add" auth="funtionOpen:add">添加</button>

            </div>

        </script>
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit" auth="funtionOpen:edit">编辑</a>
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete" auth="funtionOpen:delete">删除</a>
        </script>
    </div>
</div>

<script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="/js/common.js?v=5" charset="utf-8"></script>
<script src="/xm-select-v1.2.4/xm-select.js"></script>

<script>
    var serverGroup = JSON.parse(localStorage.getItem('services'))

    new Vue({
        el: "#app",
        data: {
            services: JSON.parse(localStorage.getItem('services')),
            whereConditions: {}

        },
        mounted:function () {
            var _this = this;
            layui.use(['form', 'table','laydate'], function () {
                var isLodedTable = false;
                var $ = layui.jquery,
                    form = layui.form,
                    table = layui.table
                    ,laydate=layui.laydate;

                laydate.render({
                    elem: '#createTime0',
                    value: getYmd(0)
                });
                laydate.render({
                    elem: '#createTime1',
                    value: getYmd(1)
                });
                var dataSg = [];
                for(var k in serverGroup){
                    var v = serverGroup[k];
                    dataSg.push({
                        name:v.name,
                        value:v.server_id,
                    })
                }

                // 渲染 xmSelect
                xmSelect.render({
                    el: '#serverGroup',
                    name: "serverGroup",
                    language: 'zn',
                    data: dataSg,
                    on: (data) => {
                        // 选择器值发生变化时触发
                        console.log('DATA:', data);
                        var selectedValues = data.arr.map(item => item.value);
                        console.log('选中的参数', selectedValues);
                        // 更新查询条件
                        _this.whereConditions.serverIds = selectedValues; // 使用 this 访问 Vue 实例
                    }
                });



                var loadTable = function(){
                    table.render({
                        where:  _this.whereConditions,
                        elem: '#currentTableId',
                        url: '/admin/funtionOpen/paginate',
                        defaultToolbar: ['filter', 'print' ],
                        toolbar: '#toolbarDemo',
                        cols: [[
                            {type: "checkbox", width: 50},

                            {field: 'id', title:"id", width: 100},
                            {field: 'serverId', title:"区服ID", width: 100},
                            {title: '名称',width: 150, field:"name"},
                            {title: '关闭模式',width: 150, field:"close_type"},
                            {title:"开启等级 ",width: 100,field: 'open_level'  },
                            {title:"开启类型",width: 100,field: 'open_type'  },
                            {field: 'open_value1', title: '值1(货币类型)',templet:function(row){
                                    if(row.open_value1===1) return '华夏币'
                                    if(row.open_value1===2) return '银元'
                                    if(row.open_value1===3) return '元宝'
                                    return '未知'
                                }},
                            {title:"值2",field: 'open_value2' ,},
                            {title:"吃鸡模式",width: 100,field: 'IsCloseChiji'  },
                            {title:"竞技场",width: 100,field: 'isCloseArena'  },
                            {title:"跨服大厅",width: 100,field: 'isCloseCrossserver'  },

                            {title: '操作', minWidth: 100, toolbar: '#currentTableBar', align: "center"}
                        ]],
                        limits: [10, 15, 20, 25, 50, 100],
                        limit: 15,
                        page: true,
                        skin: 'line'
                    }); // 绑定 this 以便访问 Vue 实例;
                    isLodedTable = true;
                }

                loadTable();
                // 监听搜索操作
                form.on('submit(data-search-btn)', function (data) {

                    if (!isLodedTable){
                        loadTable();
                    } else {
                        //执行搜索重载
                        table.reload('currentTableId', {
                            page: {
                                curr: 1
                            }
                            , where: _this.whereConditions // 使用更新后的查询条件
                        }, 'data');
                    }
                    return false;
                }); // 绑定 this 以便在回调中访问 Vue 实例);



                table.on('toolbar(currentTableFilter)',function (obj) {
                    var selectedData = table.checkStatus(obj.config.id).data;
                    var serverIds = selectedData.map(function(data) {
                        return data.server_id;
                    }).join(',');
                    var names = selectedData.map(function(data) {
                        return data.name;
                    }).join('|');
                    var namesEncoded = encodeURIComponent(names);
                    console.log('查看标题',obj.event)
                    if(obj.event === 'add'){ //添加
                        console.log('进来没：');
                        layer.open({
                            type: 2, // page 层类型
                            area: ['780px', '700px'],
                            title: '添加区服',
                            shade: 0.6, // 遮罩透明度
                            shadeClose: true,
                            maxmin: true, // 允许全屏最小化
                            content: "detail.html"
                        });
                    }

                })

                table.on('tool(currentTableFilter)', function (obj) {
                    var data = obj.data;
                    if (obj.event==='edit'){
                        layer.open({
                            type: 2, // page 层类型
                            area: ['780px', '700px'],
                            title: '编辑区服',
                            shade: 0.6, // 遮罩透明度
                            shadeClose: true,
                            maxmin: true, // 允许全屏最小化
                            content: "detail.html?id="+data.serverId +"&ids="+data.id
                        });
                    } else if(obj.event==='delete'){
                        layer.confirm('您确定删除吗?',{title:"提示"},function(){
                            $.post('/funtionOpen/server/'+data.id,{_method:'delete'},function(res){
                                if (res.code === 0) {
                                    layer.msg('success', {icon: 6, time: 500}, function () {
                                        location.reload();//父页面刷新
                                    })
                                } else {
                                    layer.msg(res.msg, {icon: 5})
                                }
                            })
                        })
                    }
                });

            });
        }
    })

</script>
</body>
</html>